package com.depository.common.utils.crypto;

import java.security.PrivateKey;
import java.security.PublicKey;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;

public class SignatureUtilsTest {

//商户验存管系统同理
	@Test
	private void testmd1() throws Exception {

		// 1.生成密钥对
		/*KeyPair keyPair = SignatureUtils.generateRsaKeyPair(2048);
		// 2.生成公私钥匙base64
		String privateStr = Base64.encodeBase64String(keyPair.getPrivate().getEncoded());// 商户留着签名
		String publicStr = Base64.encodeBase64String(keyPair.getPublic().getEncoded());// 发给存管系统
		System.out.println("privateStr==========>"+privateStr);
		System.out.println("publicStr==========>"+publicStr);*/
		
		String privateStr="MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCubtC33Wh8E/VTamdS1jN9/xcXKcDawWihhBv95pV5DvSJd6XegfBe/DG3zfFMHWgmEZXDlgO8PGUA7QdU7u7E1tuCdJ3G529MFf0qxTsXMK8xbV9Mb0qV7Z2GqaTxKvXkBY+cTZNzFWjSGUATl4BZtFpYbMGFCRxRH07KT9PS18VjaDkOJmh98tw4lAFcjaXhSSumi305Rexai60w6MBE1xMLUMsHgEY18N9kAUlvttv1c0ygj4C8iJl92Py76bQJCj0L6fQe6fXVceLQhPMq5hvmtEvL+hwN9+hEcEV1srR8e/WtTYue/uSfDc9eCS+PcPSwEl4yCZRDrH+5l067AgMBAAECggEAdFElEVatdAADGcrcd5qq35wZE7MeDUHnubzUxazbg8x7OKf2jbfsSAq4RK8p9u2tI6WaTsBcYUuMtt9YgkSKJ+x2mDmXdKmjyALpvXmj+8TVLoqWf+u+utemscOyX7ecHJ+wsjPw3U8rhN6dofLm/eqMTGbJbSM0JMxRyAsPX4qyRMMJmAYFk+NkSwxhx4pJRpMx6iypDuh2EDB9oXpRYk9eOK1SloBGPapv2n/Cv15QikULGh5yPoV+HLdP+rIFAb9kmYTWBwVzZIGEB+e67IQsps4P36D/fdcsz7DPqUzfqn3tRsVoo/WTevAo6wPY+krpGgVuJMx4hgFX3+A8wQKB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cUR9Oyk/T0tfFY2g5DiZoffLcOJQBXI2l4Ukrpot9OUXsWoutMOjARNcTC1DLB4BGNfDfZAFJb7bb9XNMoI+AvIiZfdj8u+m0CQo9C+n0Hun11XHi0ITzKuYb5rRLy/ocDffoRHBFdbK0fHv1rU2Lnv7knw3PXgkvj3D0sBJeMgmUQ6x/uZdOuwIDAQAB";
		// 3.公私钥匙生成
		PrivateKey privateKey = SignatureUtils.getRsaPkcs8PrivateKey(Base64.decodeBase64(privateStr));
		PublicKey publicKey = SignatureUtils.getRsaX509PublicKey(Base64.decodeBase64(publicStr));
		
		//签名
		String content = "{\"platformUserNo\":\"123\",\"requestNo\":\"34234234\",\"realName\":\"陈少聪\",\"idCardType\":\"G2_IDCARD\",\"userRole\":\"20150312135520\",\"idCardNo\":\"220102199203120812\",\"mobile\":\"18611936074\",\"bankcardNo\":\"6228480402564890018\",\"callbackUrl\":\"http://requestb.in/1jaym0m1\",\"notifyUrl\":\"http://requestb.in/ojkzvdoj\",\"timestamp\":\"20151112134411\"}";
		byte[] sign = SignatureUtils.sign(SignatureAlgorithm.SHA1WithRSA, privateKey, content);
		String sign64 = Base64.encodeBase64String(sign);//报文中的sign
		System.out.println(sign64);

		//存管系统根据商户发来的公钥验签
		boolean b = SignatureUtils.verify(SignatureAlgorithm.SHA1WithRSA, publicKey, content, sign);
		System.out.println(b);
	}

}
